package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.ClientTrace;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.TransferHistory;
import cn.wolfcode.crm.mapper.ClientMapper;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.mapper.TransferHistoryMapper;
import cn.wolfcode.crm.query.ClientQueryObject;
import cn.wolfcode.crm.service.IClientService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ClientServiceImpl implements IClientService {
    @Autowired
    private ClientMapper clientMapper;

    @Autowired
    private TransferHistoryMapper transferHistoryMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void updateSaleMan(ClientTrace clientTrace) {
        clientMapper.updateSaleMan(clientTrace.getClient().getId(), clientTrace.getSaleman().getId());
    }

    /**
     * 根据条件查询客户列表导出到exl
     *
     * @param qo
     * @return
     */
    @Override
    public Workbook exportXls(ClientQueryObject qo) {
        //创建xls
        Workbook book = new HSSFWorkbook();
        //创建工作簿
        Sheet sheet = book.createSheet();
        //设置第一行的标题
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("用户名");
        row.createCell(1).setCellValue("营销人员");
        row.createCell(2).setCellValue("跟踪次数");
        row.createCell(3).setCellValue("最后跟踪时间");
        row.createCell(4).setCellValue("约访日期");
        row.createCell(5).setCellValue("下次跟进时间");
        row.createCell(6).setCellValue("QQ");
        row.createCell(7).setCellValue("电话");
        row.createCell(8).setCellValue("学校");
        row.createCell(9).setCellValue("意向程度");
        row.createCell(10).setCellValue("意向校区");
        row.createCell(11).setCellValue("意向班级");
        row.createCell(12).setCellValue("状态");
        row.createCell(13).setCellValue("是否跟踪");
        row.createCell(14).setCellValue("备注");
        List<?> list = clientMapper.query4List(qo);
        for (int i = 0; i < list.size(); i++) {
            try {
                Client client = (Client) list.get(i);
                //创建行
                row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(client.getName());
                row.createCell(1).setCellValue(client.getSaleman().getUsername());
                row.createCell(2).setCellValue(client.getTraceNum());
                row.createCell(3).setCellValue(client.getLastTraceTime());
                row.createCell(4).setCellValue(client.getAppointTime());
                row.createCell(5).setCellValue(client.getQq());
                row.createCell(6).setCellValue(client.getTel());
                row.createCell(7).setCellValue(client.getSchool());
                row.createCell(8).setCellValue(client.getIntentionDegree().getName());
                row.createCell(9).setCellValue(client.getCampus().getName());
                row.createCell(10).setCellValue(client.getClz().getName());
                row.createCell(11).setCellValue(client.getState().getName());
                row.createCell(12).setCellValue(client.isTraced() ? "是" : "否");
                row.createCell(13).setCellValue(client.getRemark());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return book;
    }

    public void changeState(Long clientId) {
        clientMapper.changeState(clientId);
    }

    public void traced(Long id) {
        Date date = new Date();
        clientMapper.traced(id, date);
    }

    public void abandon(Long clientId) {
        clientMapper.abandon(clientId);
    }

    public List<Client> selectAllPoolClient() {
        return clientMapper.selectAllPoolClient();
    }

    public void dispatch(Long clientId, Long salemanId) {
        clientMapper.join(clientId, salemanId);
        Employee employee = employeeMapper.selectByPrimaryKey(salemanId);
        transfer(clientId, employee);
    }

    public void join(Long clientId) {
        //获取当前用户信息
        Subject subject = SecurityUtils.getSubject();
        Employee principal = (Employee) subject.getPrincipal();
        clientMapper.join(clientId, principal.getId());
        transfer(clientId, principal);
    }

    public void transfer(Long clientId, Employee employee) {
        //保存到移交历史表
        Client client = clientMapper.selectByPrimaryKey(clientId);
        TransferHistory history = new TransferHistory();
        history.setClient(client);
        history.setNewSaleman(client.getSaleman().getUsername());
        history.setOldSaleman(employee.getUsername());
        history.setTransferDate(new Date());
        transferHistoryMapper.insert(history);
    }

    //通过状态查询所有正式学员
    @Override
    public List<Client> selectAllStudentOfficial() {
        return clientMapper.selectAllStudentOfficial();
    }

    @Override
    public void deleteByPrimaryKey(Long id) {
        clientMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(Client entity) {
        //Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        //entity.setSaleman(employee);
        clientMapper.insert(entity);
    }

    @Override
    public Client selectByPrimaryKey(Long id) {

        return clientMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Client> selectAll() {
        return clientMapper.selectAll();
    }

    @Override
    public void updateByPrimaryKey(Client entity) {
        clientMapper.updateByPrimaryKey(entity);
    }

    @Override
    public PageResult query(ClientQueryObject qo) {
        Integer total = clientMapper.query4Count(qo);
        if (total == 0) {
            return PageResult.PAGERESULT_EMPTY;
        }
        return new PageResult(total, clientMapper.query4List(qo));
    }
}
